<template>
  <div class="container-post">
    <Breadcrumb :items="['讨论社区', '首页']" />
    <a-row :gutter="16" class="content-row">
      <a-col :span="17" class="left-col">
        <content-list :zone="currentTab" />
      </a-col>
      <a-col :span="7" class="right-col">
        <a-space direction="vertical" :size="16" style="width: 100%">
          <ul class="right-side">
            <li class="search-wrapper">
              <icon-search class="search-icon" />
              <a-input
                class="search-input"
                :style="{ width: '180px' }"
                placeholder="搜索帖子"
                allow-clear
                @click="handleOpenSearchModal"
              />
            </li>
            <li>
              <a-button
                type="primary"
                class="publish-btn"
                @click="handleCreatePost"
              >
                <template #icon>
                  <icon-plus />
                </template>
                发布帖子
              </a-button>
            </li>
          </ul>
          <div class="side-section">
            <div class="section-header">必读榜单</div>
            <post-hot-list />
          </div>
          <div class="side-section">
            <div class="section-header">分类导航</div>
            <tab-list :current-tab="currentTab" @tab-value-change="handleTabChange" />
          </div>
        </a-space>
      </a-col>
    </a-row>
  </div>
  <a-modal :visible="searchModalVisible" @cancel="handleCancelSearchModal">
    <template #title>搜索帖子</template>
    <a-space direction="vertical" fill>
      <a-input-search
        v-model="postTitle"
        size="large"
        placeholder="请输入标题以搜索"
        @search="handleSearch"
        @keydown.enter="handleSearch"
      />
      <search-post-list
        ref="searchRef"
        @close-modal="handleCloseModalByChild"
      />
    </a-space>
    <template #footer>
      <a-button @click="handleCancelSearchModal">取消</a-button>
    </template>
  </a-modal>
</template>

<script setup lang="ts">
import { ref, unref, onMounted, nextTick, onBeforeUnmount } from 'vue';
import ContentList from '@/views/post/view/components/content-list.vue';
import PostHotList from '@/views/post/view/components/post-hot-list.vue';
import TabList from '@/views/post/view/components/tab-list.vue';
import SearchPostList from '@/components/navbar/components/search-post-list.vue';
import { useRouter } from 'vue-router';

const currentTab = ref('synthesis');
const router = useRouter();
const handleTabChange = (value: string) => {
  currentTab.value = value;
  // 当分类变化时，调整高度
  nextTick(() => {
    adjustContentHeight();
  });
};
const handleCreatePost = () => {
  router.push({ name: 'CreatePost' });
};

// 调整内容区域高度
const adjustContentHeight = () => {
  const rightColElement = document.querySelector('.right-col');
  const leftColElement = document.querySelector('.left-col');
  const contentCardElement = document.querySelector('.content-card');

  if (rightColElement && leftColElement && contentCardElement) {
    const rightColHeight = rightColElement.getBoundingClientRect().height;
    (contentCardElement as HTMLElement).style.height = `${rightColHeight}px`;
  }
};

// 在组件挂载后和窗口大小变化时调整高度
onMounted(() => {
  nextTick(() => {
    adjustContentHeight();
    window.addEventListener('resize', adjustContentHeight);
  });
});

// 组件卸载前移除事件监听器
onBeforeUnmount(() => {
  window.removeEventListener('resize', adjustContentHeight);
});

const searchModalVisible = ref(false);
const postTitle = ref('');
const searchRef = ref<any>();
// 执行搜索
const handleSearch = () => {
  searchRef.value.searchPost(unref(postTitle));
};
// 点击帖子，关闭搜索框
const handleCloseModalByChild = (flag: boolean) => {
  if (flag) {
    searchModalVisible.value = false;
  }
};
const handleCancelSearchModal = () => {
  searchModalVisible.value = false;
};
const handleOpenSearchModal = () => {
  searchModalVisible.value = true;
};
</script>

<style scoped lang="less">
.container-post {
  padding: 0 20px 20px;
}

.content-row {
  display: flex;
  
  .left-col, .right-col {
    display: flex;
    flex-direction: column;
  }
  
  .left-col {
    height: 100%;
    
    :deep(.content-card) {
      flex: 1;
      margin-bottom: 0;
      box-sizing: border-box;
      height: 100%;
    }
  }
}

.right-side {
  display: flex;
  padding-right: 20px;
  list-style: none;
  align-items: center;
  margin: 0;

  li {
    display: flex;
    align-items: center;
    padding: 0 10px;
  }

  .search-wrapper {
    position: relative;
    display: flex;
    align-items: center;
    padding: 4px;
    border: 1px solid #2759cd;
    border-radius: 22px;
    background-color: var(--color-fill-2);
    transition: all 0.3s ease;

    &:hover {
      box-shadow: 0 0 0 2px rgba(39, 89, 205, 0.1);
    }

    .search-icon {
      position: absolute;
      left: 20px;
      font-size: 16px;
      color: #2759cd;
    }

    .search-input {
      padding-left: 40px;
      border-radius: 20px;
      background-color: transparent;

      :deep(.arco-input-inner-wrapper) {
        background-color: transparent;
        border: none;

        &:hover,
        &:focus {
          background-color: var(--color-fill-3);
        }

        input {
          background-color: transparent;

          &::placeholder {
            color: rgba(39, 89, 205, 0.6);
          }
        }
      }
    }
  }
}

.publish-btn {
  display: flex;
  align-items: center;
  height: 32px;
  border-radius: 16px;
  padding: 0 16px;
  background-color: #2759cd;

  &:hover {
    background-color: #1e4aa3;
  }

  :deep(.arco-icon) {
    font-size: 14px;
    margin-right: 6px;
  }
}

.side-section {
  background-color: #ffffff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(39, 89, 205, 0.08);
  }

  .section-header {
    padding: 16px;
    font-size: 18px;
    font-weight: 600;
    color: #2758cd;
    background-color: rgb(39 89 205 / 15%);
  }
}

:deep(.side-card) {
  border-radius: 4px;

  .arco-card-header {
    border-bottom: none;
    padding: 16px;
  }

  .arco-card-body {
    padding: 0 16px 16px;
  }
}
</style>
